<template>
  <div v-if="desktop.hasCustomDesktop">
    <a-spin :spinning="spinning" :delay="delayTime">
      <DesktopHome v-if="desktop.hasCustomDesktop" :list="desktop.list" />
    </a-spin>
  </div>
  <div class="p-4" v-else>
    <div class="md:flex enter-y">
      <div class="welcome">欢迎进入...</div>
    </div>
  </div>
</template>
<script lang="ts" setup>
  import { reactive, ref, watch } from 'vue';
  import DesktopHome from '/@/views/generator/desktop/components/DesktopHome.vue';
  import { useUserStore } from '/@/store/modules/user';
  import { useTabs } from '/@/hooks/web/useTabs';
  import { useTitle } from '@vueuse/core';
  import { message } from 'ant-design-vue';
  import { checkToken } from '/@/api/checkToken';
  import { getMenus } from '/@/utils/menu'
  import { useRouter } from 'vue-router';
  import { getMenuPermission } from '/@/api/checkToken';
  const loading = ref(true);
  const desktop = reactive({ hasCustomDesktop: false, list: [] });

  const spinning = ref<boolean>(true);
  const delayTime = 500;
  setTimeout(() => {
    loading.value = false;
    spinning.value = false;
  }, 1000);

  const userStore = useUserStore();
  const userInfo = userStore.getUserInfo;
  const { setTitle } = useTabs();

  watch(
    () => userInfo.desktopSchema,
    (v) => {
      getList(v);
    },
    {
      deep: true,
    },
  );
  getList(userInfo.desktopSchema);
  function getList(desktopSchema) {
    if (desktopSchema?.jsonContent) {
      desktop.list = JSON.parse(desktopSchema.jsonContent);
      //设置Tab标题
      setTitle(desktopSchema.name);
      //设置浏览器标题
      useTitle(` ${desktopSchema.name} `);
      desktop.hasCustomDesktop = true;
    }
  }
  const route = useRouter();
  if (window.location.href.split('?token=').length === 2) {
    console.log('进1');
    let queryUrl = window.location.href.split('?token=')[1];
    let token = '';
    if (queryUrl.split('&').length === 2) {
      token = queryUrl.split('&')[0];
    } else {
      token = queryUrl;
    }
    console.log(token, 'token');
    localStorage.removeItem('tabsList');
    checkToken({
      token: token,
    }).then((res) => {
        localStorage.setItem('token', res.token);
        localStorage.setItem('userId', res.userId);
        userStore.afterLoginAction(true);
        // getMenus()
        getMenuPermission(res.userId).then((res) => {
          localStorage.setItem('menuPermissionList', res.menuPermission);
          setTimeout(() => {
            route.push('/sourcingBigData/priceSourcing');
          }, 2000);
        }).catch(() => {});
      })
      .catch(() => {});
  } else {
    // message.error('无效的用户信息');
    // userStore.logout(true)
    console.log('进2');
    setTimeout(() => {
      route.replace('/sourcingBigData/priceSourcing');
    }, 2000);
  }
</script>
<style lang="less" scope>
  .welcome {
    font-size: 24px;
    font-weight: 600;
  }
</style>
